package com.ecommerce.onion.infrastracture.repository.mongo;

import com.ecommerce.onion.domain.Order;
import com.ecommerce.onion.domain.repository.OrderRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

import java.util.Optional;
import java.util.UUID;
/**
 * @packageName: infrastracture.repository.mongo（基础设施.资料库卡.mongo）
 * @className: MongoDbOrderRepository(MongoDb订单资料库)
 * @description: MongoDB存储方式
 * @author: luds
 * @version: v1.0
 * @date: 2021-03/26
 */
@Component
@Primary
public class MongoDbOrderRepository implements OrderRepository {

    /**
     * 使用SpringData实现的MongoDb订单资料库
     */
    private final SpringDataMongoOrderRepository orderRepository;

    /**
     * SpringData实现的MongoDb资料库来构建MongoDb订单资料库
     * @param orderRepository 订单资料库
     */
    @Autowired
    public MongoDbOrderRepository(final SpringDataMongoOrderRepository orderRepository) {
        this.orderRepository = orderRepository;
    }

    /**
     * 根据ID查找订单
     * @param id 订单ID
     * @return 返回订单实例
     */
    @Override
    public Optional<Order> findById(final UUID id) {
        return orderRepository.findById(id);
    }

    /**
     * 持久化订单
     * @param order 订单实例
     */
    @Override
    public void save(final Order order) {
        orderRepository.save(order);
    }
}
